package yxy.game.pm2.command.therionEquipment;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.GodBeastEquipPosition;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;
import yxy.game.pm2.module.godBeast.TherionEquipHandler;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author wrs
 * @since 2022-07-28
 * @descrition 神兽装备熔炼
 */
@MessageType(proto = Command.TherionEquipmentSmelt ,classtype = PickedGodBeastMessage.TherionEquipmentSmeltRequest.class)
public class CMD_TherionEquipmentSmelt extends Command<PickedGodBeastMessage.TherionEquipmentSmeltRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.TherionEquipmentSmeltRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int therionType = request.getTherionType(); //神兽Type
        int equippos = request.getEquippos(); //神兽装备部位
        List<Integer> equipIds = request.getEquipIdsList(); //消耗的装备ID

        PickedGodBeastMessage.TherionEquipmentSmeltResponse.Builder response = PickedGodBeastMessage.TherionEquipmentSmeltResponse.newBuilder();

        TherionEquipHandler handler = new TherionEquipHandler();

        try {
            PickedGodBeastModule pgbModule = PickedGodBeastModule.get(playerCode);
            if (pgbModule == null) {
                send(messageSource, response, Result.DataError);//不存在,参数错误
                return;
            }

            GodBeast godBeast = pgbModule.getGbs().get(String.valueOf(therionType));
            if (godBeast == null) {
                send(messageSource, response, Result.DataError);//不存在,参数错误
                return;
            }

            GodBeastEquipPosition equipPosition = GodBeastEquipPosition.forNumber(equippos); //神兽装备部位
            if (equipPosition == null){
                send(messageSource, response, Result.ParamError);
                return;
            }

            //todo 判断装备等级是否满级
            boolean flag = handler.positionLevelIsUpper(godBeast,equipPosition,equippos);
            if (flag){
                send(messageSource,response, Result.TherionPositionLevelUpper); //神兽部位等级达到上限
            }

            if (equipIds.size() == 0){
                send(messageSource,response, Result.SelectConsumeEquips); //请选择消耗的装备
                return;
            }

            Map<Integer, Integer> consumeNumMap = handler.getConsumeEquipNum(equipIds); //各类装备的消耗数量，key：装备ID value：消耗数量

            //todo 消耗选择的装备，根据表转化为对应的经验值
            // 判断经验值值是否足够 提升强化等级，足够就提升
            if (handler.consumeEquips(godBeast, equipPosition, consumeNumMap, playerCode)){
                pgbModule.set();
            }else {
                send(messageSource,response, Result.PackItemNumberLack);
            }

            //todo 重新获取神兽装备、查询表中下一强化等级的增幅 返回给前端
            Collection<Currency> allEquips = Item.all(playerCode, CurrencyType.CT_装备).values();
            if (allEquips == null){
                send(messageSource,response, Result.DataError);
                return;
            }

            ArrayList<Currency> therionEquips = handler.getTherionEquipsInPackage(allEquips);

            Result result = handler.packTherionEquipsAndNextLevelIncrease(godBeast,equipPosition,therionEquips,response);

            send(messageSource,response,result);

            PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
            PlayTypeHandle.savePickedGodBeast(playerCode,playTypeModule);
            new BattleHandle().powerUpdate(playerCode);// 战力更新
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.TherionEquipmentSmeltResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
